package com.system.util;

/** 
 * <p>unicode 编码解码工具类<p> 
 * @author qsmx 
 * 
 */  
public class UnicodeUtil {  
  
    /** 
     * <p>转为unicode 编码<p> 
     * @param string 
     * @return unicodeString 
     *//*  
    public static String encode(String str) {  
        String prifix = "\\u";  
        StringBuffer unicode = new StringBuffer();  
        for (int i = 0; i < str.length(); i++) {  
            char c = str.charAt(i);  
            String code = prifix + format(Integer.toHexString(c));  
            unicode.append(code);  
        }  
        return unicode.toString();  
    }  
    */
	
    /** 
     * <p>unicode 解码<p> 
     * @param unicode 
     * @return originalString 
     */
    public static String decode(String unicode) {  
        StringBuffer str = new StringBuffer();  
        String[] hex = unicode.split("\\\\u");  
        for (int i = 1; i < hex.length; i++) {  
            int data = Integer.parseInt(hex[i], 16);  
            str.append((char) data);  
        }  
        return str.length() > 0 ? str.toString() : unicode;  
    }  
    
    /** 
     * <p>转为数据库查询编码<p> 
     * 向数据库查询时,\\u需要转为% 
     * @param str 
     * @return 
     *//*  
    public static String encodeDBSearchParam(String str) {  
        str = encode(str);  
        str = str.replace("\\", "%");  
        return str;  
    }  
      
    *//** 
     * <p>解码数据库查询参数<p> 
     * 数据库查询后,回传参数% 转回\\u 
     * @param str 
     * @return 
     *//*  
    public static String decodeDBSearchParam(String str) {  
        str = str.replace("%", "\\");  
        str = decode(str);  
        return str;  
    }  
      
    *//** 
     * 为长度不足4位的unicode 值补零 
     * @param str 
     * @return 
     *//*  
    private static String format(String str) {  
        for ( int i=0, l=4-str.length(); i<l; i++ )   
            str = "0" + str;  
        return str;  
    }  
	*/
	/**
	 * Unicode编码转汉字
	 * @Author 于泓磊
	 * @date   2017年12月8日
	 * @param theString
	 * @return
	 */
	public static String decodeUnicode(String theString) {  
		  
		   char aChar;  
		  
		   int len = theString.length();  
		  
		   StringBuffer outBuffer = new StringBuffer(len);  
		  
		   for (int x = 0; x < len;) {  
		  
		   aChar = theString.charAt(x++);  
		  
		   if (aChar == '\\') {  
		  
		    aChar = theString.charAt(x++);  
		  
		    if (aChar == 'u') {  
		  
		    // Read the xxxx  
		  
		    int value = 0;  
		  
		    for (int i = 0; i < 4; i++) {  
		  
		     aChar = theString.charAt(x++);  
		  
		     switch (aChar) {  
		  
		     case '0':  
		  
		     case '1':  
		  
		     case '2':  
		  
		     case '3':  
		  
		    case '4':  
		  
		     case '5':  
		  
		     case '6':  
		      case '7':  
		      case '8':  
		      case '9':  
		      value = (value << 4) + aChar - '0';  
		      break;  
		      case 'a':  
		      case 'b':  
		      case 'c':  
		      case 'd':  
		      case 'e':  
		      case 'f':  
		      value = (value << 4) + 10 + aChar - 'a';  
		      break;  
		      case 'A':  
		      case 'B':  
		      case 'C':  
		      case 'D':  
		      case 'E':  
		      case 'F':  
		      value = (value << 4) + 10 + aChar - 'A';  
		      break;  
		      default:  
		      throw new IllegalArgumentException(  
		       "Malformed  \\uxxxx  encoding.");  
		      }  
		  
		     }  
		     outBuffer.append((char) value);  
		     } /*else {  
		     if (aChar == 't')  
		      aChar = '\t';  
		     else if (aChar == 'r')  
		      aChar = '\r';  
		  
		     else if (aChar == 'n')  
		  
		      aChar = '\n';  
		  
		     else if (aChar == 'f')  
		  
		      aChar = '\f';  
		  
		     outBuffer.append(aChar);  
		  
		     }  */
		  
		    } else 
		  
		    outBuffer.append(aChar);  
		  
		    }  
		  
		    return outBuffer.toString();  
		  
		   }
	
	public  static  String convert(String utfString){  
	    StringBuilder sb = new StringBuilder();  
	    int i = -1;  
	    int pos = 0;  
	      
	    while((i=utfString.indexOf("\\u", pos)) != -1){  
	        sb.append(utfString.substring(pos, i));  
	        if(i+5 < utfString.length()){  
	            pos = i+6;  
	            sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));  
	        }  
	    }  
	      
	    return sb.toString();  
	} 
  
}  